package com.semeureka.fault.bean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.semeureka.fault.bean.Device.Phase;
import com.semeureka.fault.util.Message;
import com.semeureka.frame.util.HexUtils;

@Entity
@Table(name = "T_PDXL_RAWDATA")
public class Rawdata implements Message {
	@Id
	@GeneratedValue
	private Long id;
	@Column(name = "RAWDATA_CONTENT")
	private byte[] content;
	@Column(name = "RAWDATA_TYPE")
	private Integer type;
	@ManyToOne
	@JoinColumn(name = "GROUP_ID")
	private Group group;
	@Column(name = "CREATE_TIME")
	private Date createTime;
	@Transient
	private List<Message> messages;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public byte[] getContent() {
		return content;
	}

	public void setContent(byte[] content) {
		this.content = content;
	}

	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	@Override
	public Group getGroup() {
		return group;
	}

	public void setGroup(Group group) {
		this.group = group;
	}

	@Override
	public Phase getPhase() {
		return Phase.M;
	}

	@Override
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public List<Message> getMessages() {
		return messages;
	}

	public void setMessages(List<Message> messages) {
		this.messages = messages;
	}

	public void addMessage(Message message) {
		if (messages == null) {
			messages = new ArrayList<Message>();
		}
		messages.add(message);
	}

	public Rawdata() {
	}

	public Rawdata(byte[] content, Integer type, Device device, Date createTime) {
		this.content = content;
		this.type = type;
		if (device != null) {
			this.group = device.getGroup();
		}
		this.createTime = createTime;
	}

	public Rawdata(Integer type, Group group) {
		this.type = type;
		this.group = group;
	}

	public Rawdata(byte[] content, Integer type, Group group, Date createTime) {
		this.content = content;
		this.type = type;
		this.group = group;
		this.createTime = createTime;
	}

	@Override
	public String toString() {
		return "Rawdata [id=" + id + ", content=" + (content != null ? HexUtils.toHex(content) : null) + ", type="
				+ type + ", group=" + group + ", createTime=" + createTime + "]";
	}
}
